package servlet;


import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.ItemFacadeRemote;
import static servlet.ServletUtil.*;

/**
 * Questa classe rappresenta la servlet principale, che riceve tutte le richieste
 * http.
 *
 * @author Rachid Ouchary, Fabio Strocco, Max Passaghe, Marco Tansini.
 */
public class OldTest extends HttpServlet {
    /*@EJB
    private UtenteFacadeRemote utenteFacade;
    @EJB
    private WishFacadeRemote wishFacade;*/
    @EJB
    private ItemFacadeRemote itemFacade;
    private boolean first = true;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String operation = request.getParameter(OPERATION);
        try {
            if(operation == null) {
                //DA RIMUOVERE!!!
                if(first) {
                    HttpSession session = request.getSession();
                    //itemFacade.create(new Item(30, "Macbook"));
                    //session.setAttribute("tmp_id", ((Item) itemFacade.findBy(30, "Macbook")).getId());
                    first = false;
                }
                //END DA RIMUOVERE
                /*request.getRequestDispatcher("index_jsp.old.jsp").
                        forward(request, response);*/
                request.getRequestDispatcher("index.jsp").
                        forward(request, response);
            } else if(operation.equals(ACTION_LOGIN) ||
                      operation.equals(ACTION_REGISTRAZIONE) ||
                      operation.equals(ACTION_LOGOUT)) {
                request.getRequestDispatcher("GestoreLogin").
                        forward(request, response);
            } else if(operation.equals(ACTION_AGGIUNGI_AMICO) ||
                      operation.equals(ACTION_CERCA_AMICI)) {
                boolean logged = requestLogin(request, response);
                if(logged)
                    request.getRequestDispatcher("GestoreAmici").
                            forward(request, response);
            } else if(operation.equals(ACTION_AGGIUNGI_DESIDERIO) ||
                      operation.equals(ACTION_CREA_DESIDERIO)) {
                boolean logged = requestLogin(request, response);
                if(logged)
                    request.getRequestDispatcher("GestoreWishList").
                            forward(request, response);
            }

            /*
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet MainDispatcher</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet MainDispatcher at " + request.getContextPath () + "</h1>");
            out.println("<h3>Now creating a new wish</h3>");
            wishFacade.create(new Wish(10, 10, new Item(10, "")));
            out.println("<h3>Create succesfully completed</h3>");
            out.println("<h3>Now creating a new wish</h3>");
            wishFacade.create(new Wish(10, 30, new Item(10, "")));
            out.println("<h3>Create succesfully completed</h3>");
            out.println("<h3>" + wishFacade.findAll() + "</h3>");
            for(Wish wish : wishFacade.findAll()) {
                wishFacade.remove(wish);
            }
            out.println("<h3>Remotion succesfully completed</h3>");
            Utente u1 = new Utente("user1", "0000", "Fabio", "Strocco", "Torino");
            utenteFacade.create(u1);
            u1.aggiungiDesiderio(new Wish(10, 20, new Item(10, "Desc")));
            utenteFacade.edit(u1);
            Utente u2 = new Utente("user2", "0000", "Rachid", "Ouchary", "Asti");
            utenteFacade.create(u2);
            Utente u3 = new Utente("user3", "0000", "Marco", "Tansini", "Torino");
            utenteFacade.create(u3);
            u1 = utenteFacade.find(u1.getUsername());
            u2 = utenteFacade.find(u2.getUsername());
            u3 = utenteFacade.find(u3.getUsername());
            u1.aggiungiAmico(u2);
            u1.aggiungiAmico(u3);
            u2.aggiungiAmico(u1);
            u3.aggiungiAmico(u1);
            u1.aggiungiDesiderio(new Wish(10, 30, new Item(11, "Desc")));
            u2.aggiungiDesiderio(new Wish(10, 30, new Item(100, "D")));
            utenteFacade.edit(u1);
            utenteFacade.edit(u2);
            utenteFacade.edit(u3);
            out.println("<h3>" + u3.getUsername() + " esiste in DB? " +
                    utenteFacade.exists(u3) + "</h3>");
            out.println("user1 login status: " + utenteFacade.carica("user1", "0001") +
                    "</h3>");
            out.println("user1 login status: " + utenteFacade.carica("user1", "0000") +
                    "</h3>");
            out.println("<h3>" + toListStr(utenteFacade.findAll()) + "</h3>");
            out.println("<h3>FINE</h3>");
            out.println("</body>");
            out.println("</html>");
             */
        }
        finally {
            out.close();
        }
    }

    private boolean requestLogin(HttpServletRequest request,
                                 HttpServletResponse response)
                                           throws ServletException, IOException{
         HttpSession session = request.getSession();
         if(session.getAttribute(SESSION_UTENTE) == null){
             //Forward login page
             request.getRequestDispatcher("index_jsp.old.jsp").forward(request, response);
             return false;
         }

         return true;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
